//
//  MenuViewController.m
//  svqj
//
//  Created by xftt on 14-6-23.
//
//

#import "MenuViewController.h"
#import "MenuCellView.h"
#import "MainViewController.h"
@interface MenuViewController ()<UIAlertViewDelegate>
{
    NSMutableArray *_menuList;
    UIScrollView *_scroll;
    UIView *_numView;
    UIView *_menuView;
    UILabel *_lbNum;
}
@end

@implementation MenuViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    _menuList = [NSMutableArray array];
    _menuView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 748)];
    [_menuView setBackgroundColor:[UIColor colorWithHexString:@"#343434"]];
    UIImageView *_imgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 180, 50)];
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, 180, 698)];
    NSString* docPath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
    NSString* imagePath = [NSString stringWithFormat: @"%@/logo.png", docPath];
    [_imgv setImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]]];
    [_menuView addSubview:_imgv];
    [_menuView addSubview:_scroll];
    _numView = [[UIView alloc] initWithFrame:CGRectMake(115, 295, 18, 18)];
    [_numView setBackgroundColor:[UIColor colorWithHexString:@"#770000"]];
    _numView.layer.cornerRadius = 9;
    [_numView setUserInteractionEnabled:NO];
    [_numView setHidden:YES];
    _lbNum = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    [_lbNum setFont:[UIFont systemFontOfSize:14]];
    [_lbNum setTextColor:[UIColor colorWithHexString:@"#AAAAAA"]];
    [_lbNum setTextAlignment:NSTextAlignmentCenter];
    [_numView addSubview:_lbNum];
    _bgSelectImage = [[UIImageView alloc]  initWithFrame:CGRectMake(0, 79, 180, 44)];
    [_bgSelectImage setImage:[UIImage imageNamed:@"MenuSelectImage"]];
    [self loadMenuList];
    [self.view addSubview:_menuView];
}
- (void)showUpdataNum
{
    int num = [((MainViewController*)self.tabFrameController).update count];
    if (num > 0) {
    [_numView setHidden:NO];
    [_lbNum setText:[NSString stringWithFormat:@"%d",num]];
    }else{
        [_numView setHidden:YES];
    }
}

- (void)loadMenuList
{
    [_scroll removeSubviews];
    [_scroll addSubview:_bgSelectImage];
    [self loadMenuData];
    CGFloat y = 0;
    for (int i=0; i<[_menuList count]; i++) {
        UIView *v = [self getMenuCellWithY:y AtIndex:i];
        [_scroll addSubview:v];
        y = v.endPointY;
    }
    [_scroll addSubview:_numView];
    [_scroll setContentSize:CGSizeMake(180, [_menuList count]*44-20)];
}

- (void)loadMenuData
{
    [_menuList removeAllObjects];
    NSArray *titleKey = @[@"menuType",@"menuImage"];
    NSArray *menuKey = @[@"menuType",@"menuName",@"menuValue",@"menuIcon"];
    NSDictionary *menu0 = [[NSDictionary alloc] initWithObjects:@[@"0",[UIImage imageNamed:@"MenuTitle1"]] forKeys:titleKey];
    [_menuList addObject:menu0];
    NSDictionary *menu3 = [[NSDictionary alloc] initWithObjects:@[@"1",@"我的全景",@"2",[UIImage imageNamed:@"1"]] forKeys:menuKey];
    [_menuList addObject:menu3];
    NSDictionary *menu2 = [[NSDictionary alloc] initWithObjects:@[@"1",@"最近浏览",@"0",[UIImage imageNamed:@"2"]] forKeys:menuKey];
    [_menuList addObject:menu2];
    NSDictionary *menu1 = [[NSDictionary alloc] initWithObjects:@[@"1",@"未分类全景",@"1",[UIImage imageNamed:@"3"]] forKeys:menuKey];
    [_menuList addObject:menu1];
    NSDictionary *menu4 = [[NSDictionary alloc] initWithObjects:@[@"1",@"添加分类",@"10",[UIImage imageNamed:@"4"]] forKeys:menuKey];
    [_menuList addObject:menu4];
    NSDictionary *menu5 = [[NSDictionary alloc] initWithObjects:@[@"0",[UIImage imageNamed:@"MenuTitle2"]] forKeys:titleKey];
    [_menuList addObject:menu5];
    NSDictionary *menu6 = [[NSDictionary alloc] initWithObjects:@[@"1",@"下载全景",@"3",[UIImage imageNamed:@"5"]] forKeys:menuKey];
    [_menuList addObject:menu6];
    NSDictionary *menu7 = [[NSDictionary alloc] initWithObjects:@[@"1",@"下载/更新",@"4",[UIImage imageNamed:@"6"]] forKeys:menuKey];
    [_menuList addObject:menu7];
    NSDictionary *menu11 = [[NSDictionary alloc] initWithObjects:@[@"0",[UIImage imageNamed:@"MenuTitle4"]] forKeys:titleKey];
    [_menuList addObject:menu11];
    NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
    NSArray *_mList = [_Data objectForKey:@"menuList"];
    for (int i = 0; i<[_mList count]; i++){
    NSDictionary *dMenu = [_mList objectAtIndex:i];
    NSDictionary *menu12 = [[NSDictionary alloc] initWithObjects:@[@"1",[dMenu objectForKey:@"title"],[NSString stringWithFormat:@"%d",5+i],[UIImage imageNamed:@"9"]] forKeys:menuKey];
    [_menuList addObject:menu12];
    }
}

- (MenuCellView *)getMenuCellWithY:(CGFloat)y AtIndex:(NSInteger)index
{
    MenuCellView *view = [[MenuCellView alloc]initWithFrame:CGRectMake(0,y, 180, 44)];
    NSDictionary *_menu = [_menuList objectAtIndex:index];
    NSString *menuType = [_menu objectForKey:@"menuType"];
    [view setMenuType:menuType];
    if ([menuType isEqualToString:@"0"]){
    UIImage *image = [_menu objectForKey:@"menuImage"];
    [view setFrame:CGRectMake(0, y, 180, 35)];
    [view setMenuImage:image];
    }else if([menuType isEqualToString:@"1"]){
    NSString *text = [_menu objectForKey:@"menuName"];
    NSString *value = [_menu objectForKey:@"menuValue"];
    UIImage *icon = [_menu objectForKey:@"menuIcon"];
    UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuSelect:)];
    [view addGestureRecognizer:reg];
    [view setText:text];
    [view setValue:value];
    [view setMenuIcon:icon];
    }return view;
}

- (void)menuSelect:(UIGestureRecognizer *)sender
{
    void (^action)() = ^(){
        [UIView animateWithDuration:0.1
                     animations:^{
                         _bgSelectImage.frame = sender.view.frame;
                     }
                     completion:^(BOOL finished) {
                     }];
    };
    NSInteger index = [((MenuCellView*)sender.view).value intValue];
    switch (index) {
        case 10:{
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"添加分类"
                                                        message:@"请输入要添加的分类名称"
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"添加", nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        alert.delegate = self;
        [alert show];
        break;
        }case 11:{
        MainViewController *main = (MainViewController*)self.tabFrameController;
        [main showSN];
        break;
        }
        default:
        [self.tabFrameController setSelectedIndex:index];
        action();
        break;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        UITextField *txtSort = [alertView textFieldAtIndex:0];
        if ([txtSort.text length]>0) {
            NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
            NSArray*QJSort = [_Data objectForKey:@"QJSort"];
            NSMutableArray *sortArr = [NSMutableArray arrayWithArray:QJSort];
            NSInteger sid = -1;
            for(id sortT in QJSort){
                NSInteger _id = [[sortT objectForKey:@"sortid"] integerValue];
                if (_id>sid) {
                    sid = _id;
                }
            }
            NSMutableDictionary *sort = [NSMutableDictionary dictionary];
            [sort setObject:[NSString stringWithFormat:@"%ld",(long)(sid+1)]forKey:@"sortid"];
            [sort setObject:txtSort.text forKey:@"sortname"];
            [sortArr addObject:sort];
            [_Data setObject:sortArr forKey:@"QJSort"];
            [_Data synchronize];
            MainViewController *main = (MainViewController*)self.tabFrameController;
            [main reLoadSortView];
        }else{
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"添加分类"
                                                            message:@"分类名称不能为空"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
